package cn.tuling.org.controller;

import cn.tuling.basic.util.AjaxResult;
import cn.tuling.basic.util.PageList;
import cn.tuling.org.domain.Department;
import cn.tuling.org.query.DepartmentQuery;
import cn.tuling.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @GetMapping
    public List<Department> findAll(){

        return departmentService.findAll();
    }

    @DeleteMapping("/{dept_id}")
    public AjaxResult delete(@PathVariable("dept_id") Long deptId){
        departmentService.delete(deptId);
        return AjaxResult.me().success();
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        if (null == department.getDeptId()){
            departmentService.add(department);
        } else {
            departmentService.update(department);
        }
        return AjaxResult.me().success();
    }

    @GetMapping("/{dept_id}")
    public Department findById(@PathVariable("dept_id") Long deptId){
        return departmentService.findById(deptId);
    }

    @PostMapping
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query){

        return departmentService.queryByPage(query);
    }

}
